package com.ysd.lis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ysd.common.Result;
import com.ysd.jdbc.JdbcConnectionManager;
import com.ysd.lis.entity.DataBaseBo;
import com.ysd.lis.entity.WkCalcDatabase;
import com.ysd.lis.enums.DataSourceEnum;
import com.ysd.lis.mapper.WkCalcDatabaseMapper;
import com.ysd.lis.service.WkCalcDatabaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 绩效考核-数据源字典表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-04-18
 */
@Service
public class WkCalcDatabaseServiceImpl extends ServiceImpl<WkCalcDatabaseMapper, WkCalcDatabase> implements WkCalcDatabaseService {

    @Autowired
    WkCalcDatabaseMapper wkCalcDatabaseMapper;

    @Override
    public Result getDataBaseList(String keyWord) {
        LambdaQueryWrapper<WkCalcDatabase> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkCalcDatabase::getDelFlag, 0)
                .and(StrUtil.isNotEmpty(keyWord), wrapper -> wrapper.like(WkCalcDatabase::getDatabaseCode, keyWord).or().like(WkCalcDatabase::getDatabaseName, keyWord));
        List<WkCalcDatabase> wkCalcDatabases =wkCalcDatabaseMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", wkCalcDatabases);
    }

    @Override
    public Result addDatabase(WkCalcDatabase wkCalcDatabase) {
        boolean save = this.save(wkCalcDatabase);
        if (save) {
            return Result.succ(1, "添加成功！", save);
        } else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result editDatabase(WkCalcDatabase wkCalcDatabase) {
        boolean save = this.updateById(wkCalcDatabase);
        if (save) {
            return Result.succ(1, "编辑成功！", save);
        } else {
            return Result.fail("编辑失败");
        }
    }

    @Override
    public Result deleteDatabase(List<String> ids) {
        boolean save = this.removeByIds(ids);
        if (save) {
            return Result.succ(1, "删除成功！", save);
        } else {
            return Result.fail("删除失败");
        }
    }

    @Override
    public Result testDatabaseConnection(WkCalcDatabase wkCalcDatabase) {
        Result result = Result.succ(1, "连接成功！", "ok");
        try {
            DataSourceEnum anEnum = DataSourceEnum.getEnum(wkCalcDatabase);
            DataBaseBo dataBaseBo = anEnum.getFunc().apply(anEnum, wkCalcDatabase);

            if(anEnum.getDataSourceType().equals(999L)){
                return Result.fail("数据库类型错误!");
            }
            JdbcConnectionManager jdbcConnectionManager = new JdbcConnectionManager();
            Connection connection = jdbcConnectionManager.getConnection(dataBaseBo);
            if(connection == null){
                result = Result.fail("数据库连接失败，请查看连接信息或者数据服务状态！");
            }
            jdbcConnectionManager.closeConnection(connection);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("连接失败，请查看连接信息或者数据服务状态！");
        }
        return result;
    }

    // 创建数据源
    private DataSource createDataSource(WkCalcDatabase config) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("oracle.jdbc.OracleDriver");
        dataSource.setUrl("jdbc:oracle:thin:"+config.getHost() +":" + config.getPort() +":" + "lis");
        dataSource.setUsername(config.getUsername());
        dataSource.setPassword(config.getPassword());
        return dataSource;
    }


    private void demo(){
        //从数据库拿到
        WkCalcDatabase wkCalcDatabase = new WkCalcDatabase();

        //组装bo
        DataSourceEnum anEnum = DataSourceEnum.getEnum(wkCalcDatabase);
        DataBaseBo dataBaseBo = anEnum.getFunc().apply(anEnum, wkCalcDatabase);


        JdbcConnectionManager jdbcConnectionManager = new JdbcConnectionManager();
        Connection connection = jdbcConnectionManager.getConnection(dataBaseBo);
        jdbcConnectionManager.executeQuery(connection, "SELECT * FROM SYSTEM_LIS.WK_CALC_DATABASE");
        jdbcConnectionManager.closeConnection(connection);

//        //获取instance
//        JdbcConnectionManager instance = JdbcConnectionManager.getInstance();
//        //获取连接
//        instance.getConnection(dataBaseBo);

//        //无参数查询
//        List<Map<String, Object>> maps = instance.executeQuery("SELECT * FROM SYSTEM_LIS.WK_CALC_DATABASE ");
        //有参数查询
//        List<Map<String, Object>> maps = instance.executeQuery("SELECT * FROM SYSTEM_LIS.WK_CALC_DATABASE WHERE USERNAME = ? ", "system_lis");
//        // 最后要关闭连接
//        instance.closeConnection();
    }

}
